package com.relytosoft.mizar.media.analyze.services.controller.face;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.analyze.api.entity.face.AyFaceDisposition;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyFaceDispositionDto;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyFaceDispositionPlanDto;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.SearchFaceCaptureDto;
import com.relytosoft.mizar.media.analyze.api.pojo.query.AyFaceSearchQuery;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyFaceDispositionVo;
import com.relytosoft.mizar.media.analyze.services.service.face.AyFaceDispositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.simpleframework.xml.core.Validate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/20 10:28
 */
@RestController
@RequestMapping("analyze/face")
@Api(tags = "人脸应用")
public class AyFaceDispositionController {
    @Resource
    private AyFaceDispositionService ayFaceDispositionService;

    @SaCheckPermission("face:disposition:list")
    @OpLog(msg = "查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("查询")
    @GetMapping("list")
    public ResponseResult<Map<String, Object>> list(AyFaceDispositionDto ayFaceDispositionDto,
                                                    PageQuery pageQuery) {
        AyFaceDisposition ayFaceDisposition = BeanCopyUtils.copy(ayFaceDispositionDto, AyFaceDisposition.class);
        IPage<AyFaceDisposition> page = ayFaceDispositionService.selectAyFaceDispositionPageList(pageQuery, ayFaceDisposition);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @SaCheckPermission("face:disposition:add")
    @OpLog(msg = "添加", opType = OpTypeEnum.INSERT)
    @ApiOperation("添加")
    @PostMapping("add")
    public ResponseResult<Void> add(@RequestBody AyFaceDispositionDto ayFaceDispositionDto) {
        //获取需要添加布控任务的数据
        AyFaceDisposition ayFaceDisposition = ayFaceDispositionService.getAyFaceDispositionInfo(ayFaceDispositionDto);
        //添加布控任务到数据库
        AyFaceDisposition addSuccess = ayFaceDispositionService.addAyFaceDisposition(ayFaceDisposition);
        if (addSuccess == null) {
            return ResponseResult.fail("添加布控任务失败");
        } else {
            //发送消息添加布控任务
            ayFaceDispositionService.sendCreateMsg(addSuccess);
            return ResponseResult.ok("添加布控任务成功");
        }
    }

    @SaCheckPermission("face:disposition:update")
    @OpLog(msg = "修改", opType = OpTypeEnum.UPDATE)
    @ApiOperation("修改")
    @PutMapping("update")
    public ResponseResult<Void> update(@RequestBody AyFaceDispositionDto ayFaceDispositionDto) {
        boolean isUpdateSuccess = ayFaceDispositionService.updateAyFaceDisposition(ayFaceDispositionDto);
        if (isUpdateSuccess) {
            return ResponseResult.ok("修改布控任务成功");
        } else {
            return ResponseResult.fail("修改布控任务失败");
        }
    }


    @SaCheckPermission("face:disposition:remove")
    @OpLog(msg = "删除", opType = OpTypeEnum.DELETE)
    @ApiOperation("删除")
    @DeleteMapping("remove")
    public ResponseResult<Map<Integer, String>> remove(@RequestBody List<Integer> list) {
        return ResponseResult.ok(ayFaceDispositionService.removeAyFaceDisposition(list));
    }


    @SaCheckPermission("face:disposition:getDetails")
    @OpLog(msg = "详情", opType = OpTypeEnum.SELECT)
    @ApiOperation("详情")
    @GetMapping("{id}")
    public ResponseResult<AyFaceDispositionVo> getDetails(@PathVariable Integer id) {
        return ResponseResult.ok(ayFaceDispositionService.selectAyFaceDispositionById(id));
    }


    @SaCheckPermission("face:disposition:stop")
    @OpLog(msg = "停止布控任务", opType = OpTypeEnum.UPDATE)
    @ApiOperation("停止布控任务")
    @GetMapping("stop/{id}")
    public ResponseResult<Void> stopDispositionTask(@PathVariable Integer id) {
        boolean isStopTask = ayFaceDispositionService.stopTask(id);
        if (isStopTask) {
            return ResponseResult.ok("停止任务成功");
        } else {
            return ResponseResult.fail("当前任务未启动");
        }
    }

    @SaCheckPermission("face:disposition:start")
    @OpLog(msg = "启用布控任务", opType = OpTypeEnum.UPDATE)
    @ApiOperation("启用布控任务")
    @GetMapping("start/{id}")
    public ResponseResult<Void> startDispositionTask(@PathVariable Integer id) {
        return ResponseResult.ok(ayFaceDispositionService.startTask(id));
    }

//    @SaCheckPermission("face:disposition:armedAndDisarmed")
//    @OpLog(msg = "布防撤防", opType = OpTypeEnum.UPDATE)
//    @ApiOperation("布防撤防")
//    @PostMapping("armed/disarmed")
//    public ResponseResult<Void> armedAndDisarmed(@RequestBody List<Integer> list) {
//        boolean isSuccess = ayFaceDispositionService.armedAndDisarmed(list);
//        if (isSuccess) {
//            return ResponseResult.ok("布/撤防成功");
//        } else {
//            return ResponseResult.fail("布/撤防失败");
//        }
//    }

    @SaCheckPermission("face:disposition:search")
    @OpLog(msg = "人脸检索", opType = OpTypeEnum.SELECT)
    @ApiOperation("人脸检索")
    @GetMapping("face/search")
    public ResponseResult<JSONObject> faceSearch(@Validate SearchFaceCaptureDto searchFaceCaptureDto, AyFaceSearchQuery ayFaceSearchQuery) {
        return ResponseResult.ok(ayFaceDispositionService.faceSearch(searchFaceCaptureDto, ayFaceSearchQuery));
    }


    @ApiOperation("临时人脸库布防")
    @PostMapping("temp")
    public ResponseResult<Void> addTempFace(@RequestBody AyFaceDispositionDto ayFaceDispositionDto) {
        boolean face = ayFaceDispositionService.addTempFace(ayFaceDispositionDto);
        if (face) {
            return ResponseResult.ok("临时人脸库布防成功");
        } else {
            return ResponseResult.fail("临时人脸库布防失败");
        }
    }

    @SaCheckPermission("face:disposition:plan:add")
    @OpLog(msg = "人脸布控计划添加", opType = OpTypeEnum.INSERT)
    @ApiOperation("人脸布控计划添加")
    @PostMapping("addFaceDispositionPlan")
    public ResponseResult<Void> addFaceDispositionPlan(@Validate @RequestBody AyFaceDispositionPlanDto ayFaceDispositionPlanDto) {
        boolean face = ayFaceDispositionService.addFaceDispositionPlan(ayFaceDispositionPlanDto);
        if (face) {
            return ResponseResult.ok("人脸布控计划添加成功");
        } else {
            return ResponseResult.fail("人脸布控计划添加失败");
        }
    }


    @SaCheckPermission("face:disposition:plan:remove")
    @OpLog(msg = "人脸布控计划删除", opType = OpTypeEnum.DELETE)
    @ApiOperation("人脸布控计划删除")
    @PutMapping("removeFaceDispositionPlan")
    public ResponseResult<Void> removeFaceDispositionPlan(@RequestBody List<Integer> faceDispositionIds) {
        boolean face = ayFaceDispositionService.removeFaceDispositionPlan(faceDispositionIds);
        if (face) {
            return ResponseResult.ok("人脸布控计划删除成功");
        } else {
            return ResponseResult.fail("人脸布控计划删除失败");
        }
    }
}
